package com.ciwong.epaper.modules.epaper.ui.newlistenspeak.shortcoversation.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.epaper.bean.Attachments;
import com.ciwong.epaper.modules.epaper.bean.OptionDetail;
import com.ciwong.epaper.modules.epaper.bean.OptionInfo;
import com.ciwong.epaper.modules.epaper.bean.Questions;
import com.ciwong.epaper.util.ESystem;
import com.ciwong.mobilelib.utils.BaseJumpManager;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import static android.view.Gravity.CENTER_VERTICAL;


/**
 * date :2018/5/3
 * author: loco
 * description: 文字和图片 展示
 * version:
 * email: lzh838330255@163.com
 */

public class ImageTextOptionLoayout extends BaseOptionsLayout {

    private Context context;
    private List<OptionDetail> value;

    public ImageTextOptionLoayout(Context context) {
        super(context);
    }

    public ImageTextOptionLoayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public ImageTextOptionLoayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        this.setOrientation(VERTICAL);
    }

    /**
     * 每一个选项
     *
     * @param optionInfo 单个选项信息
     * @param context    为单个选项创建的容器
     * @return
     */
    @Override
    protected View singleOptionContent(OptionInfo optionInfo, Context context) {
        //先展示文字后展示图片（哪个不存在不展示哪个）
//单个选项item 中内容
        LinearLayout singleOptionContentll = new LinearLayout(context);
        singleOptionContentll.setOrientation(VERTICAL);
        if (optionInfo == null || optionInfo.getValue() == null || optionInfo.getValue().isEmpty())
            return null;
        value = optionInfo.getValue();
        String body = value.get(0).getBody();
        boolean flagBody = (body != null && !TextUtils.isEmpty(body));//选项中内容文字存在 为true
        List<Attachments> attachmentList = value.get(0).getAttachments();
        if (flagBody) {
            LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            TextView viewOptionContent = new TextView(this.context);
            viewOptionContent.setPadding(dip2px(this.context, SIZE_15), dip2px(this.context, SIZE_0), dip2px(this.context, SIZE_0), dip2px(this.context, SIZE_0));
            viewOptionContent.setGravity(CENTER_VERTICAL);
            viewOptionContent.setTextColor(getResources().getColorStateList(R.color.check_textview_option_tittle));
            viewOptionContent.setText(body);
            viewOptionContent.setTextSize(SIZE_15);
            viewOptionContent.setLayoutParams(layoutParams);
            singleOptionContentll.addView(viewOptionContent);
        }

        if (attachmentList != null && !attachmentList.isEmpty()) {
            ImageView imageView = initImageView(flagBody);
            Attachments attachments = attachmentList.get(0);
            final String url = attachments.getUrl();
            if (!TextUtils.isEmpty(url)) {
                switch (attachments.getFileType()) {
                    case Attachments.AttachmentFileType.ATTACHMENT_FILE_TYPE_IMG:// 图片
                        imageView.setOnClickListener(
                                new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        ArrayList<String> urlList = new ArrayList<String>();
                                        urlList.add(ESystem.formatPath(url));
                                        BaseJumpManager.jumpToScanImage((Activity) getContext(), R.string.listen_speak, urlList, 0);
                                    }
                                });
                        ImageLoader.getInstance().displayImage(ESystem.formatPath(url), imageView);
                        singleOptionContentll.addView(imageView);
                        break;
                    case Attachments.AttachmentFileType.ATTACHMENT_FILE_TYPE_AUDIO:// 语音
                        break;
                }
            }
        }

        return singleOptionContentll;
    }

    /**
     * 每一个小题的 标题
     *
     * @param question 单个小题信息
     * @param context  上下文
     * @return
     */
    @Override
    protected View loadOptionTittleLayout(Questions question, Context context) {

        LinearLayout llOptionTittle = new LinearLayout(context);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//        layoutParams.setMargins(dip2px(context, SIZE_0),dip2px(context, SIZE_0),dip2px(context, SIZE_0),dip2px(context, SIZE_21));
        llOptionTittle.setLayoutParams(layoutParams);
        llOptionTittle.setPadding(dip2px(context, SIZE_15),dip2px(context, SIZE_0),dip2px(context, SIZE_0),dip2px(context, SIZE_10));

        llOptionTittle.setOrientation(VERTICAL);
        OptionDetail optionsDetail = question.getTrunk();
        String texttitle = null;
        if (optionsDetail == null) return llOptionTittle;
        //小题之间的线
        View view = new View(context);
        view.setBackgroundColor(getResources().getColor(R.color.color_E7E7E7));
        LinearLayout.LayoutParams layoutParamsView = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dip2px(context, SIZE_1));
        view.setLayoutParams(layoutParamsView);

        TextView textView = new TextView(context);

        LinearLayout.LayoutParams layoutParamsTextView = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParamsTextView.setMargins(dip2px(context, SIZE_0),dip2px(context, SIZE_21),dip2px(context, SIZE_0),dip2px(context, SIZE_0));
        textView.setLayoutParams(layoutParamsTextView);
//        textView.setPadding(dip2px(context, SIZE_0), dip2px(context, SIZE_10), dip2px(context, SIZE_0), dip2px(context, SIZE_15));
        textView.setGravity(CENTER_VERTICAL);
        textView.setTextSize(SIZE_18);
        textView.setTextColor(getResources().getColorStateList(R.color.check_textview_option_tittle));

        if (question.getSort() != null && !"".equals(question.getSort())) {
            texttitle = question.getSort() + "、" + optionsDetail.getBody();
        } else {
            texttitle = optionsDetail.getBody();
        }
        textView.setText(texttitle);
        textView.setSelected(optionsDetail.isHighlight());
        if (optionsDetail.isHighlight())
            textView.setTypeface(Typeface.DEFAULT_BOLD);
        else
            textView.setTypeface(Typeface.DEFAULT);
        llOptionTittle.addView(view);
        llOptionTittle.addView(textView);

        // 取附件集合
        final List<Attachments> attachmentsList = optionsDetail.getAttachments();
        if (attachmentsList != null && !attachmentsList.isEmpty()) {
            for (int i = 0; i < attachmentsList.size(); i++) {
                final String url = attachmentsList.get(i).getUrl();
                if (!TextUtils.isEmpty(url)) {
                    switch (attachmentsList.get(i).getFileType()) {
                        case Attachments.AttachmentFileType.ATTACHMENT_FILE_TYPE_IMG:// 图片
                            ImageView imageView = initTittleImageView();
                            imageView.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    ArrayList<String> urlList = new ArrayList<String>();
                                    urlList.add(ESystem.formatPath(url));
                                    BaseJumpManager.jumpToScanImage((Activity) getContext(), R.string.listen_speak, urlList, 0);
                                }
                            });
                            ImageLoader.getInstance().displayImage(ESystem.formatPath(url), imageView);
                            llOptionTittle.addView(imageView);
                            break;
                        case Attachments.AttachmentFileType.ATTACHMENT_FILE_TYPE_AUDIO:// 语音
                            break;
                    }
                }
            }
        }
        return llOptionTittle;
    }

    /**
     * 创建小题题干imageview
     *
     * @return
     */
    private ImageView initTittleImageView() {
        ImageView imageView = new ImageView(context);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dip2px(context, SIZE_145));
        layoutParams.topMargin= dip2px(context, SIZE_16);
        imageView.setLayoutParams(layoutParams);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        return imageView;
    }

    /**
     * 创建imageview
     *
     * @param flagBody 文字选项是否存在
     * @return
     */
    private ImageView initImageView(boolean flagBody) {
        LinearLayout.LayoutParams layoutParams = null;
        ImageView imageView = new ImageView(context);
        if (flagBody) {//文字选项是存在时 图片展示 与文字有间距
            layoutParams = new LinearLayout.LayoutParams(dip2px(context, SIZE_51), dip2px(context, SIZE_51));
//            layoutParams.leftMargin = dip2px(context, SIZE_10);
            layoutParams.topMargin = dip2px(context, SIZE_6);
        } else {
            layoutParams = new LinearLayout.LayoutParams(dip2px(context, SIZE_51), dip2px(context, SIZE_51));
        }
        imageView.setLayoutParams(layoutParams);
        imageView.setPadding(dip2px(context, SIZE_15), dip2px(context, SIZE_0), dip2px(context, SIZE_0), dip2px(context, SIZE_0));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        return imageView;
    }
}
